#################################
##' Code for:
##' Title: "Effective climate clubs require ambition, leverage, and insulation"
##' Author: Sam S. Rowan
##' Contact: sam.rowan [at] concordia.ca
##' Date: 2023-10-19
#################################
##' This script:
##' Robustness: re-weight export exposure by exports/GDP
#################################

## Working directory
# setwd("/Users/srowa/Dropbox/Projects/climate_clubs_trade")
# -- Set working directory to replication folder

## Need objects from '06_ideal_type_clubs.R'
source("06_ideal_type_clubs.R")

## Need the following objects 
# total_ndc_exposure
# total_leverage_exposure
# total_eu_exposure


#### Re-weight exports into the club by exports share ####

## For NDC club
total_ndc_exposure <- total_ndc_exposure %>% 
  # Then re-weight export exposure by exports as a share of GDP
  left_join(., select(wdi_emissions, iso3c, exports), by = c("reporter_iso" = "iso3c")) %>% 
  mutate(export_exposure_to_club_pct = export_exposure_to_club * exports/100) %>% 
  as.data.frame()

## And plot
total_ndc_exposure %>% 
  full_join(., wdi_emissions, by = c("reporter_iso" = "iso3c")) %>% 
  mutate(reporter_in_club = as.factor(case_when(reporter_in_club==1 ~ "Member",
                                                reporter_in_club!=1 ~ "Non-member"))) %>% 
  mutate(reporter_in_club = relevel(reporter_in_club,  ref = "Non-member")) %>% 
  ggplot(., aes(x = ghg_multiplicative_log, y = export_exposure_to_club_pct,
                color = reporter_in_club)) +
  geom_smooth(method = "lm", se = F) +
  geom_text(size = 5, aes(label = reporter_iso)) +
  scale_alpha_manual(values = c(1, 1)) +
  # scale_y_continuous(limits = c(0,1), breaks = seq(0, 1, 0.1)) +
  scale_color_manual(values = c("#556B2F", "grey70")) +
  theme_clubs +
  theme(legend.position = "none") +
  labs(y = expression(paste("Exports to club" %*% " Exports share of GDP")),
       color = "Club member",
       x = expression(paste("GHG emissions" %*% "GHG emissions per capita, log transformed")))

## For leverage club
total_leverage_exposure <- total_leverage_exposure %>% 
  # Then re-weight export exposure by exports as a share of GDP
  left_join(., select(wdi_emissions, iso3c, exports), by = c("reporter_iso" = "iso3c")) %>% 
  mutate(export_exposure_to_club_pct = export_exposure_to_club * exports/100) %>% 
  as.data.frame()

## And plot
total_leverage_exposure %>% 
  full_join(., wdi_emissions, by = c("reporter_iso" = "iso3c")) %>% 
  mutate(reporter_in_club = as.factor(case_when(reporter_in_club==1 ~ "Member",
                                                reporter_in_club!=1 ~ "Non-member"))) %>% 
  mutate(reporter_in_club = relevel(reporter_in_club,  ref = "Non-member")) %>% 
  filter(!reporter_iso %in% c("LUX", "HKG")) %>% 
  ggplot(., aes(x = ghg_multiplicative_log, y = export_exposure_to_club_pct,
                color = reporter_in_club)) +
  geom_smooth(method = "lm", se = F) +
  geom_text(size = 5, aes(label = reporter_iso)) +
  # scale_y_continuous(limits = c(0,1), breaks = seq(0, 1, 0.1)) +
  scale_color_manual(values = c("#556B2F", "grey70")) +
  theme_clubs +
  theme(legend.position = "none") +
  labs(y = expression(paste("Exports to club" %*% " Exports share of GDP")),
       color = "Club member",
       x = expression(paste("GHG emissions" %*% "GHG emissions per capita, log transformed")))


## For EU club
total_eu_exposure <- total_eu_exposure %>% 
  # Then re-weight export exposure by exports as a share of GDP
  left_join(., select(wdi_emissions, iso3c, exports), by = c("reporter_iso" = "iso3c")) %>% 
  mutate(export_exposure_to_club_pct = export_exposure_to_club * exports/100) %>% 
  as.data.frame()

## And plot
total_eu_exposure %>% 
  full_join(., wdi_emissions, by = c("reporter_iso" = "iso3c")) %>% 
  mutate(reporter_in_club = as.factor(case_when(reporter_in_club==1 ~ "Member",
                                                reporter_in_club!=1 ~ "Non-member"))) %>% 
  mutate(reporter_in_club = relevel(reporter_in_club,  ref = "Non-member")) %>% 
  filter(!reporter_iso %in% c("LUX")) %>% 
  ggplot(., aes(x = ghg_multiplicative_log, y = export_exposure_to_club_pct,
                color = reporter_in_club)) +
  geom_smooth(method = "lm", se = F) +
  geom_text(size = 5, aes(label = reporter_iso)) +
  # scale_y_continuous(limits = c(0,1), breaks = seq(0, 1, 0.1)) +
  # scale_y_continuous(limits = c(0,0.5), breaks = seq(0, .5, 0.1)) +
  scale_color_manual(values = c("#556B2F", "grey70")) +
  theme_clubs +
  theme(legend.position = "none") +
  labs(y = expression(paste("Exports to club" %*% " Exports share of GDP")),
       color = "Club member",
       x = expression(paste("GHG emissions" %*% "GHG emissions per capita, log transformed")))








